package com.colin.setting;

import com.colin.ColinConstant;
import com.colin.setting.ui.DataBaseConfigUI;
import com.colin.util.NotificationUtil;
import com.colin.util.PropertiesManager;
import com.intellij.openapi.options.SearchableConfigurable;
import com.intellij.openapi.util.NlsContexts;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

public class SettingConfigurable implements SearchableConfigurable {
    private final DataBaseConfigUI form = new DataBaseConfigUI();

    @Override
    public @NotNull
    @NonNls
    String getId() {
        return ColinConstant.SETTING_ID_MYSQL;
    }

    @Override
    public @NlsContexts.ConfigurableName String getDisplayName() {
        return ColinConstant.SETTING_NAME_MYSQL;
    }

    @Override
    public @Nullable
    JComponent createComponent() {

        JPanel jPanel = new JPanel();
        jPanel.setLayout(new GridLayout(10, 2));
        jPanel.add(form);

        return jPanel;
    }

    @Override
    public boolean isModified() {
        return true;
    }

    @Override
    public void apply() {
        PropertiesManager.save(ColinConstant.MYSQL_HOST, form.getMysqlHost());
        PropertiesManager.save(ColinConstant.MYSQL_PORT, form.getMysqlPort());
        PropertiesManager.save(ColinConstant.MYSQL_USER_NAME, form.getUsername());
        PropertiesManager.save(ColinConstant.MYSQL_PASS_WORD, form.getPassword());
        PropertiesManager.save(ColinConstant.MYSQL_DATABASE, form.getDatabase());
        NotificationUtil.info("save success!");
    }
}
